package artandco.controller;

import java.io.IOException;

import javax.persistence.EntityManagerFactory;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import artandco.dao.ClientDAO;
import artandco.entites.Client;

/**
 * Index.java
 * 
 * @version 1.0
 * @date 26 mai 2011
 */
public class Index extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * Constructeur
	 */
	public Index() {
		super();
	}

	/**
	 * doGet
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * doPost
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
		IOException {
		response.setContentType("text/html;charset=UTF-8");
		EntityManagerFactory emf = (EntityManagerFactory) getServletContext().getAttribute("emf");
		ClientDAO clientDAO = new ClientDAO(emf);
		Client client = clientDAO.getClient(1);
		if (client == null) {
			client = new Client();
		}
		request.setAttribute("client", client);
		RequestDispatcher dispatcher = request.getRequestDispatcher("canalXHTML/index.jsp");
		dispatcher.forward(request, response);
	}

}
